package com.sz.netty.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * RabbitMQ configuration class
 */
@Configuration
public class RabbitMQConfig {
    
    // Exchange names
    public static final String DTU_TRANSPORT_EXCHANGE = "dtu.transport.exchange";
    public static final String DTU_RECORD_EXCHANGE = "dtu.record.exchange";
    
    // Queue names
    public static final String DTU_TRANSPORT_QUEUE = "dtu.transport.queue";
    public static final String DTU_RECORD_QUEUE = "dtu.record.queue";
    
    // Dead letter queue names
    public static final String DTU_TRANSPORT_DLQ = "dtu.transport.dlq";
    public static final String DTU_RECORD_DLQ = "dtu.record.dlq";
    
    // Routing keys
    public static final String DTU_TRANSPORT_ROUTING_KEY = "dtu.transport.key";
    public static final String DTU_RECORD_ROUTING_KEY = "dtu.record.key";
    public static final String DTU_TRANSPORT_DLQ_ROUTING_KEY = "dtu.transport.dlq.key";
    public static final String DTU_RECORD_DLQ_ROUTING_KEY = "dtu.record.dlq.key";
    
    /**
     * Create transport exchange
     */
    @Bean
    public DirectExchange dtuTransportExchange() {
        return new DirectExchange(DTU_TRANSPORT_EXCHANGE, true, false);
    }
    
    /**
     * Create record exchange
     */
    @Bean
    public DirectExchange dtuRecordExchange() {
        return new DirectExchange(DTU_RECORD_EXCHANGE, true, false);
    }
    
    /**
     * Create transport dead letter exchange
     */
    @Bean
    public DirectExchange dtuTransportDLX() {
        return new DirectExchange("dtu.transport.dlx", true, false);
    }
    
    /**
     * Create record dead letter exchange
     */
    @Bean
    public DirectExchange dtuRecordDLX() {
        return new DirectExchange("dtu.record.dlx", true, false);
    }
    
    /**
     * Create transport queue with dead letter configuration (TTL removed)
     */
    @Bean
    public Queue dtuTransportQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", "dtu.transport.dlx");
        args.put("x-dead-letter-routing-key", DTU_TRANSPORT_DLQ_ROUTING_KEY);
        return new Queue(DTU_TRANSPORT_QUEUE, true, false, false, args);
    }
    
    /**
     * Create record queue with dead letter configuration (TTL removed)
     */
    @Bean
    public Queue dtuRecordQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", "dtu.record.dlx");
        args.put("x-dead-letter-routing-key", DTU_RECORD_DLQ_ROUTING_KEY);
        return new Queue(DTU_RECORD_QUEUE, true, false, false, args);
    }
    
    /**
     * Create transport dead letter queue
     */
    @Bean
    public Queue dtuTransportDLQ() {
        return new Queue(DTU_TRANSPORT_DLQ, true);
    }
    
    /**
     * Create record dead letter queue
     */
    @Bean
    public Queue dtuRecordDLQ() {
        return new Queue(DTU_RECORD_DLQ, true);
    }
    
    /**
     * Bind transport queue to transport exchange
     */
    @Bean
    public Binding dtuTransportBinding() {
        return BindingBuilder.bind(dtuTransportQueue())
                .to(dtuTransportExchange())
                .with(DTU_TRANSPORT_ROUTING_KEY);
    }
    
    /**
     * Bind record queue to record exchange
     */
    @Bean
    public Binding dtuRecordBinding() {
        return BindingBuilder.bind(dtuRecordQueue())
                .to(dtuRecordExchange())
                .with(DTU_RECORD_ROUTING_KEY);
    }
    
    /**
     * Bind transport dead letter queue to dead letter exchange
     */
    @Bean
    public Binding dtuTransportDLQBinding() {
        return BindingBuilder.bind(dtuTransportDLQ())
                .to(dtuTransportDLX())
                .with(DTU_TRANSPORT_DLQ_ROUTING_KEY);
    }
    
    /**
     * Bind record dead letter queue to dead letter exchange
     */
    @Bean
    public Binding dtuRecordDLQBinding() {
        return BindingBuilder.bind(dtuRecordDLQ())
                .to(dtuRecordDLX())
                .with(DTU_RECORD_DLQ_ROUTING_KEY);
    }
    
    /**
     * Create RabbitTemplate with JSON message converter
     */
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(new Jackson2JsonMessageConverter());
        return template;
    }
}